/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jl.tools.stkeditor.generate;

import com.jl.tools.stkeditor.exception.DataNotMatchCodingScheamException;
import com.jl.util.HexUtil;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author LuHongjun
 */
public final class Bit7Code implements Code {

  private String oneline;

  public Bit7Code(String oneline) {
    this.oneline = oneline;
  }

  @Override
  public List<String> getCode() throws DataNotMatchCodingScheamException {
    List<String> l = new ArrayList<String>();
    List<Integer> intData = new ArrayList<Integer>();
    for (int i = 0; i < oneline.length(); i++) {
      int value = oneline.codePointAt(i);
      if ((value & 0xFF00) != 0) {
        throw new DataNotMatchCodingScheamException("Data Wrong,Be Careful Of The Coding Scheam");
      } else {
        intData.add(value);
      }
    }
    calculate7Bit(intData, l);
    return l;
  }

  @Override
  public int getLength() {
    return oneline.length() - oneline.length() / 8;
  }

  private void calculate7Bit(List<Integer> intData, List<String> l) {
    for (int i = 0; i < intData.size(); i++) {
      int left = intData.get(i);
      int right = 0;
      if(i < intData.size() -1){
        right = intData.get(i + 1);
      }
      if ((i + 1) % 8 == 0) {
        continue;
      }
      int temp = (left >> (i % 8)) | (right << (7 - i % 8)) & 0xFF;

      String bit7 = HexUtil.toHexXX(temp);
      l.add(bit7);
    }
  }
  
  
}
